//
//  ByFootBallAllGoalsCell.m
//  caipiao_ios
//
//  Created by mc on 2018/7/20.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByFootBallAllGoalsCell.h"



@implementation ByFootBallAllGoalsCell

+ (instancetype)cellWithTableView:(UITableView*)tableView{
    
    static NSString *cellIdentifier = @"ByFootBallAllGoalsCell";
    ByFootBallAllGoalsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSeparatorStyleNone;
        self.contentView.backgroundColor = BY_BACKGROUND_COLOR;
        
        [self initViews];
    }
    return self;
}


- (void)initViews{
    
    [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    UIView *backgroundView = [[UIView alloc] init];
    backgroundView.layer.cornerRadius = 10;
    backgroundView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:backgroundView];
    [backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(0);
        make.left.equalTo(self.contentView).offset(5);
        make.right.equalTo(self.contentView).offset(-5);
        make.bottom.equalTo(self.contentView).offset(-10);
    }];
    
    
    ByFootBallTitleView *titleView = [[ByFootBallTitleView alloc] init];
    [backgroundView addSubview:titleView];
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backgroundView).offset(0);
        make.right.equalTo(backgroundView).offset(0);
        make.top.equalTo(backgroundView).offset(0);
        make.height.mas_equalTo(@85);
    }];
    self.titleView = titleView;
    
    UIView *buttonsView = [[UIView alloc] init];
    [backgroundView addSubview:buttonsView];
    [buttonsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backgroundView).offset(0);
        make.right.equalTo(backgroundView).offset(0);
        make.top.equalTo(titleView.mas_bottom).offset(0);
        make.height.mas_equalTo(@60);
    }];
    self.buttonsView = buttonsView;
    
}

- (void)setDataArray:(NSMutableArray *)dataArray{
    _dataArray = dataArray;
    [self.buttonsView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    CGFloat magin = 10;
    CGFloat width = (SCREEN_WIDTH - 10  - magin * 9) / 8.0;
    CGFloat height = 50;
    
    ByNetModel *model = [_dataArray firstObject];
    self.titleView.netModel = model;
    self.titleView.danguanView.hidden = model.single.integerValue == 0;
    
    for (int i = 0;i < _dataArray.count; i++) {
        ByNetModel *netModel = [_dataArray objectAtIndex:i];
        UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(magin + i % 8 * (width + magin), 0, width,height)];
        [self.buttonsView addSubview:valueLabel];
        
        valueLabel.tag = 100 + i;
        valueLabel.font = FONT(12);
        valueLabel.textAlignment = NSTextAlignmentCenter;
        valueLabel.numberOfLines = 0;
        if (!netModel.selected) {
            valueLabel.textColor = [UIColor lightGrayColor];
            valueLabel.backgroundColor = [UIColor whiteColor];
            valueLabel.layer.borderColor = BY_LINE_COLOR.CGColor;
            valueLabel.layer.borderWidth = 1;
        }else{
            valueLabel.textColor = [UIColor whiteColor];
            valueLabel.backgroundColor = BY_THEME_COLOR;
            
        }
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [valueLabel addGestureRecognizer:tap];
        if (netModel.isNoSale) {
            valueLabel.text = @"未开售";
            valueLabel.userInteractionEnabled = NO;
        }else{
            valueLabel.text = [NSString stringWithFormat:@"%@ \n %.1f",netModel.title, netModel.valueString.floatValue];
            valueLabel.userInteractionEnabled = YES;
        }
    }
}


- (void)tapAction:(UITapGestureRecognizer *)tap{
    if (self.indexAtView) {
        self.indexAtView(tap.view.tag - 100);
    }
}

@end
